package com.jule.teenpatti.mail.network.protocol.reqs;

import JoloProtobuf.AuthSvr.JoloAuth;
import com.jule.teenpatti.base.enums.ErrorCodeEnum;
import com.jule.teenpatti.mail.network.protocol.ClientReq;
import com.jule.teenpatti.mail.network.protocol.acks.JoloAuth_MailCheckAck_600022;
import com.jule.teenpatti.mail.service.MailService;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JoloAuth_MailCheckReq_600022 extends ClientReq {
    public JoloAuth_MailCheckReq_600022(int functionId) {
        super(functionId);
    }
    private JoloAuth.JoloAuth_MailCheckReq req;
    @Override
    public void readPayLoadImpl(ByteBuf buf) throws Exception {
        byte[] blob = new byte[buf.readableBytes()];
        buf.readBytes(blob);
        req = JoloAuth.JoloAuth_MailCheckReq.parseFrom(blob);
    }

    @Override
    public void processImpl() throws Exception {
        log.debug("收到消息-> " + functionId + ", reqNum-> " + header.reqNum + ", req->" + req.toString());
        JoloAuth.JoloAuth_MailCheckAck.Builder ack = JoloAuth.JoloAuth_MailCheckAck.newBuilder();
        ack.setIsRead(0).setMailId(req.getMailId()).setResult(1).setUserId(req.getUserId());
        Long mailId = req.getMailId();
        String userId = req.getUserId();
        try{
            boolean reslut = MailService.getInstance().checkMail(userId,mailId);
            ack.setIsRead(reslut?1:0);
            ctx.writeAndFlush(new JoloAuth_MailCheckAck_600022(ack.build(),header));
        }catch(Exception ex){
            ack.setResult(0);
            ack.setResultMsg(ErrorCodeEnum.GAME_50002_2.getCode());
            ctx.writeAndFlush(new JoloAuth_MailCheckAck_600022(ack.build(),header));

            log.error(ex.getMessage(),ex);
        }
    }
}
